const express = require('express');
const { SimpleData, SuccessData } = require('../common/returnDataFormat');
const {
  findOneTopic,
  findAllTopic,
  postTopic,
  upTopicViews,
  modifyTopic,
  deleteTopic,
  getPagingTopic,
  getTopicTypes,
  postComment,
  deleteComment,
  pagingTopic,
  addType,
  modifyType,
  deleteType,
  pagingType,
  findTypeById,
} = require('../controller/topicController');
const User = require('../model/user');
const { verifyToken } = require('../utils/token');
const router = express.Router();

// 通过留言id获取留言内容
router.get('/getTopicById', async (req, res) => {
  if (req.query.topic_id != 'undefined' && req.query.topic_id != '') {
    const result = await findOneTopic(req.query.topic_id);
    if (result) {
      upTopicViews(result.topic_id, result.views);
    }
    result.views++;
    res.send(SuccessData(result));
  } else {
    res.send(SimpleData(500, 'id不存在'));
  }
});

//分页查询文章列表
router.post('/getPagingTopic', async (req, res) => {
  const result = await getPagingTopic(req.body);
  res.send(result);
});
//管理页面分页
router.post('/pagingTopic', async (req, res) => {
  const result = await pagingTopic(req.body);
  res.send(SuccessData(result));
});

// 发表文章
router.post('/postTopic', async (req, res) => {
  const token = req.headers.authorization;
  const verifyRes = await verifyToken(token);
  const author_id = verifyRes.data.userId;
  const topicInfo = { ...req.body, author_id };
  const result = await postTopic(topicInfo);
  if (result.code == 200) {
    res.send(SimpleData(200, '发表文章成功'));
  } else {
    res.send(SimpleData(500, '发表文章失败'));
  }
});

//修改文章

/* 
暂时实现为等级小于且不为作者本人没有权限修改文章
  可能为后续权限系统中的版主做准备
    版主不可删除比自己权限高的用户的文章
*/
router.post('/modifyTopic', async (req, res) => {
  const { topic_id, ...body } = req.body;
  const topicInfo = await findOneTopic(topic_id);
  const token = req.headers.authorization;
  const verifyRes = await verifyToken(token);
  if (topicInfo.author_id != verifyRes.data.userId) {
    const user = await User.findOne({
      where: {
        user_id: topicInfo.author_id,
      },
      raw: true,
    });
    if (verifyRes.data.userLevel < user.user_level) {
      return res.send(SimpleData(500, '非作者不能修改文章内容'));
    }
  }
  const result = await modifyTopic(body, topic_id);
  res.send(result);
});

//删除文章
router.get('/delTopic', async (req, res) => {
  const topic_id = req.query.topic_id;
  const topicInfo = await findOneTopic(topic_id);
  const token = req.headers.authorization;
  const verifyRes = await verifyToken(token);
  if (topicInfo.author_id != verifyRes.data.userId) {
    const user = await User.findOne({
      where: {
        user_id: topicInfo.author_id,
      },
      raw: true,
    });
    if (verifyRes.data.userLevel < user.user_level) {
      return res.send(SimpleData(500, '非作者不能删除文章内容'));
    }
  }
  const result = await deleteTopic(topic_id);
  res.send(result);
});

//获取文章分类
router.get('/getTopicTypes', async (req, res) => {
  const result = await getTopicTypes();
  res.send(result);
});

// 发送评论
router.post('/postComment', async (req, res) => {
  const token = req.headers.authorization;
  if (token) {
    try {
      const verifyRes = await verifyToken(token);
      const fromId = verifyRes.data.userId;
      const fromuser = verifyRes.data.username;
      const result = await postComment({ ...req.body, fromId, fromuser });
      res.send(result);
    } catch (error) {
      res.send(error);
    }
  } else {
    res.send(SimpleData(500, 'Token不存在'));
  }
});

//删除评论
router.get('/delComment', async (req, res) => {
  const id = req.query.id;
  const result = await deleteComment(id);
  res.send(result);
});

//管理页面分页
router.post('/pagingType', async (req, res) => {
  const result = await pagingType(req.body);
  res.send(SuccessData(result));
});

router.get('/getTypeById', async (req, res) => {
  const result = await findTypeById(req.query);
  res.send(result);
});
module.exports = router;
